﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PatientsManagement.Database;
using System.Configuration;
using PatientsManagement.Users;
using PatientsManagement.Patients;
using PatientsManagement.Patients_and_Doctors;
using PatientsManagement.DoctorWindow;

namespace PatientsManagement
{
    public partial class ManageVisits : Form
    {
        DataTable dataTable = new DataTable();
        string username = "";
        int userID;
        int doctorID;
        int patientID = -1;

        public ManageVisits(string username,int userID)
        {
            InitializeComponent();
            this.userID = userID;
            this.doctorID = DoctorsData.GetDoctorID(userID);
            this.username = username;
            user.Text = UsersData.GetUserForenameSurname(username);
            FillGridWithPatientsData();
        }

        private void MainForm_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Escape)
                this.Dispose();
        }

        private void FillGridWithPatientsData()
        {
            dataView.DataSource = VisitsData.GetShortenedPatientsTable();
            int count = dataView.ColumnCount;
            columnsCombo.Items.Clear();
            dataTable = new DataTable();
            for (int i = 0; i < dataView.ColumnCount; i++)
            {
                columnsCombo.Items.Add(dataView.Columns[i].Name);
                dataTable.Columns.Add(dataView.Columns[i].Name, typeof(string));
                dataTable.Columns[i].ReadOnly = true;
                dataView.Columns[i].ReadOnly = true;
            }
            for (int i = 0; i < dataView.Rows.Count; i++)
            {
                dataTable.Rows.Add(dataView.Rows[i].Cells["ID"].Value.ToString(),
                    dataView.Rows[i].Cells["Imię"].Value.ToString(),
                    dataView.Rows[i].Cells["Nazwisko"].Value.ToString(),
                    dataView.Rows[i].Cells["Pesel"].Value.ToString(),
                    dataView.Rows[i].Cells["Wizyty"].Value.ToString());
            }
            columnsCombo.SelectedIndex = 0;
            filterValue.Text = "";
        }

        private void FillGridWithPatientsHistoryData()
        {
            if(patientID==-1)this.patientID = (int)dataView.SelectedRows[0].Cells["ID"].Value;
            dataView.DataSource = VisitsData.GetPatientVisitsHistoryTable(doctorID,patientID);
            int count = dataView.ColumnCount;
            columnsCombo.Items.Clear();
            dataTable = new DataTable();
            for (int i = 0; i < dataView.ColumnCount; i++)
            {
                columnsCombo.Items.Add(dataView.Columns[i].Name);
                dataTable.Columns.Add(dataView.Columns[i].Name, typeof(string));
                dataTable.Columns[i].ReadOnly = true;
                dataView.Columns[i].ReadOnly = true;
            }
            for (int i = 0; i < dataView.Rows.Count; i++)
            {
                dataTable.Rows.Add(dataView.Rows[i].Cells["ID_Wizyty"].Value.ToString(),
                    //dataView.Rows[i].Cells["Imię"].Value.ToString(),
                    //dataView.Rows[i].Cells["Nazwisko"].Value.ToString(),
                    dataView.Rows[i].Cells["Pesel"].Value.ToString(),
                    dataView.Rows[i].Cells["Rozpoznanie"].Value.ToString(),
                    dataView.Rows[i].Cells["Data_Wizyty"].Value.ToString(),
                    dataView.Rows[i].Cells["ID_Recepty"].Value.ToString());
            }
            columnsCombo.SelectedIndex = 0;
            filterValue.Text = "";
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void visitsButton_Click(object sender, EventArgs e)
        {
            this.patientID = -1;
            visitsButton.BackColor = Color.Silver;
            addNew.Enabled = true;
            editVisit.Enabled = false;
            delete.Enabled = false;
            visitsHistory.Enabled = true;
            patientName.Text = "";
            patientName.Visible = false;
            FillGridWithPatientsData();
        }

        private void filterButton_Click(object sender, EventArgs e)
        {
            dataTable.DefaultView.RowFilter = string.Format("{0} LIKE '%{1}%'", columnsCombo.Text, filterValue.Text);
            dataView.DataSource = dataTable;         
        }

        private void addNew_Click(object sender, EventArgs e)
        {
            if (dataView.SelectedRows.Count > 0)
            {
                if (this.patientID == -1) this.patientID = (int)dataView.SelectedRows[0].Cells["ID"].Value;
                AddVisit addVisit = new AddVisit(patientID,doctorID,patientName.Text);
                if (addVisit.ShowDialog() == DialogResult.OK)
                {
                    addVisit.AddToDatabase();
                    if (visitsButton.BackColor == Color.Silver)
                        FillGridWithPatientsData();
                    else
                        FillGridWithPatientsHistoryData();
                }
            }
        }

        private void visitsHistory_Click(object sender, EventArgs e)
        {
            if (dataView.SelectedRows.Count > 0)
            {
                visitsButton.BackColor = Color.Transparent;
                string forename = dataView.SelectedRows[0].Cells["Imię"].Value.ToString();
                string surname = dataView.SelectedRows[0].Cells["Nazwisko"].Value.ToString();
                editVisit.Enabled = true;
                delete.Enabled = true;
                visitsHistory.Enabled = false;
                patientName.Text = forename + " " + surname;
                patientName.Visible = true;
                FillGridWithPatientsHistoryData();
            }
        }

        private void delete_Click(object sender, EventArgs e)
        {
            if (dataView.SelectedRows.Count > 0)
            {
                int visitID = (int)dataView.SelectedRows[0].Cells["ID_Wizyty"].Value;
                if (DialogResult.Yes == MessageBox.Show("Na pewno chcesz usunąć wizytę o numerze " + visitID+ "?", "Usuwanie wizyty", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))
                {
                    VisitsData.DeleteVisit(visitID);
                    MessageBox.Show("Wizyta pacjenta " + patientName.Text + " o numerze " + visitID + " została usunięta.");
                    FillGridWithPatientsHistoryData();
                }
            }
        }

        private void editVisit_Click(object sender, EventArgs e)
        {
            if (dataView.SelectedRows.Count > 0)
            {
                int visitID = int.Parse(dataView.SelectedRows[0].Cells["ID_Wizyty"].Value.ToString());
                AddVisit addVisit = new AddVisit(patientID, doctorID, visitID, patientName.Text);
                if (addVisit.ShowDialog() == DialogResult.OK)
                {
                    addVisit.SaveChangesToDatabase();
                    FillGridWithPatientsHistoryData();
                }
            }
        }
    }
}
